/**
 * Created by Administrator on 2017/6/26.
 */

let schedule = require('node-schedule');
let gameData = require('../util/gameData.js');
let variable = require('../data/variable.js');
let mem = require('../util/mem.js');
let push = require('./push.js');
let file = require('../util/file.js');
let util = require('../util/util.js');
//let action = require('./action.js');

let cron = module.exports;

cron.init = function () {
    // 建立一个时间循环的默认规则
    //let rule = new schedule.RecurrenceRule();
    // 增加一个每秒执行的内容
    //let times = [];
    //for (let i = 1; i < 60; i++) {
    //    times.push(i);
    //}
    //rule.second = times;
    // 开始动作
    schedule.scheduleJob('0 * * * * *', function () {
        let l1 = Object.keys(mem.clients).length;

        let userIds = [];

        let lossTime = Date.now() - (gameData.d.config.linkLossTime * 1000);
        let lossTime2 = Date.now() - 30000;
        for (let i in mem.clients) {
            if (!mem.clients[i].user) {
                if (lossTime2 > mem.clients[i].updateTime) {
                    mem.clients[i].close();
                }
                continue;
            }
            if (lossTime > mem.clients[i].updateTime) {
                push.sendError(10004, i);
                //action.close(mem.clients[i]);
                mem.clients[i].close();
                continue;
            }
            userIds.push(mem.clients[i].user.id);
        }
        let l2 = Object.keys(mem.clients).length;

        let date = (new Date()).Format('hh:mm');

        file.append(variable.path.client, date + "\t" + util.getServerInfo('id')
            + "\t" + l1 + "\t" + l2 + "\t" + JSON.stringify(userIds) + "\n");
    });
};